<?php


namespace App\Server;

/**
 * 腾讯地图
 * Class TencentMap
 * @package App\Server
 */
class TencentMap
{
    /**
     * 根据定位获取区县
     * @param $location
     * @return string
     */
    public static function locationCity($location)
    {
        $key = env('TENCENT_MAP_API_KEY');
        $res = file_get_contents("https://apis.map.qq.com/ws/geocoder/v1/?key={$key}&location={$location}");
        $res = json_decode($res, true);
        if ($res['status'] !== 0) {
            file_put_contents("req_log.txt", "\n" . date('Y-m-d H:i:s'). "\n 腾讯地图获取定位失败，返回值: " . json_encode($res). "\n", FILE_APPEND);
            return 'nankang';
        }
        $city = $res['result']['ad_info']['district'];
        $cityCode = str_replace(['区', '县', '市'], '', $city);
        return array_key_exists($cityCode, Constants::$city) ? Constants::$city[$cityCode] : 'nankang';
    }
}